package com.leetcode.offer.chapter3;

import com.leetcode.basic.ListNode;

/**
 * @author Dennis Li
 * @date 2020/7/13 15:27
 */
public class MergeTwoLists_25 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null && l2 == null) return null;
        ListNode node = new ListNode(-1);
        ListNode dummy = new ListNode(-1);
        dummy.next = node;
        while (l1 != null || l2 != null) {
            if (l2 != null && (l1 == null || l1.val > l2.val)) {
                node.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            else {
                node.next = new ListNode(l1.val);
                l1 = l1.next;
            }
            node = node.next;
        }
        return dummy.next.next;
    }
}
